package com.medicalproject.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.medical.pojo.CleaningWorkorder;
import com.medicalproject.mapper.CleaningWorkorderMapper;
import com.medicalproject.service.CleaningWorkorderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CleaningWorkorderServiceImpl implements CleaningWorkorderService {
    @Resource
    private CleaningWorkorderMapper cleaningWorkorderMapper;

    /**
     * 获取所有保洁工单列表
     *
     * @return 保洁工单列表
     */
    @Override
    public List<CleaningWorkorder> list() {
        return cleaningWorkorderMapper.list();
    }

    /**
     * 根据次数获取保洁工单列表
     *
     * @param times 时间
     * @return 保洁工单列表
     */
    @Override
    public List<CleaningWorkorder> listtime(Integer times) {
        return cleaningWorkorderMapper.listtime(times);
    }

    /**
     * 根据优先级进行排序
     *
     * @param yid 优先级
     * @return 保洁工单列表
     */
    @Override
    public List<CleaningWorkorder> listyxj(Integer yid) {
        return cleaningWorkorderMapper.listyxl(yid);
    }

    /**
     * 根据工单类型获取清洁工单列表
     *
     * @param type 工单类型
     * @return 清洁工单列表
     */
    @Override
    public List<CleaningWorkorder> listgdlx(Integer type) {
        return cleaningWorkorderMapper.listgdlx(type);
    }

    /**
     * 根据ID查找清洁工作订单
     *
     * @param id 工作订单的唯一标识符l
     * @return 返回找到的清洁工作订单对象，如果没有找到则返回null
     */
    @Override
    public CleaningWorkorder findById(Integer id) {
        return cleaningWorkorderMapper.findbyid(id);
    }

    /**
     * 添加保洁工单信息
     * 该方法负责创建并持久化一个新的清洁工单对象它使用了Snowflake算法来生成一个唯一的工单编号，
     * 并将工单的其他信息从输入参数复制到新创建的对象中此外，它还会设置创建时间和处理状态
     *
     * @param cleaningWorkorder 包含要添加的清洁工单信息的对象
     * @return 返回清洁工单添加操作的结果，通常是一个表示成功或失败的整数
     */
    @Override
    public int add(CleaningWorkorder cleaningWorkorder) {
        // 创建Snowflake实例，用于生成唯一编号
        Snowflake snowflake = IdUtil.createSnowflake(1,1);
        // 将生成的唯一编号转换为字符串，用作清洁工单的代码
        String code = String.valueOf(snowflake.nextId());

        // 创建一个新的保洁工单对象，用于存储到数据库
        CleaningWorkorder cleaningWorkorder1 = new CleaningWorkorder();
        cleaningWorkorder1.setCode(code);
        cleaningWorkorder1.setType(cleaningWorkorder.getType());
        cleaningWorkorder1.setYid(cleaningWorkorder.getYid());
        cleaningWorkorder1.setSbname(cleaningWorkorder.getSbname());
        cleaningWorkorder1.setPhone(cleaningWorkorder.getPhone());
        cleaningWorkorder1.setUrl(cleaningWorkorder.getUrl());
        // 设置创建时间和处理状态
        cleaningWorkorder1.setSctime(LocalDateTime.now());
        cleaningWorkorder1.setGdtime(LocalDateTime.now());
        cleaningWorkorder1.setGdzt(1);
        cleaningWorkorder1.setZfzt(0);
        cleaningWorkorder1.setPlanbegintime(cleaningWorkorder.getPlanbegintime());
        cleaningWorkorder1.setTimelimit(cleaningWorkorder.getTimelimit());
        cleaningWorkorder1.setDispatchtime(cleaningWorkorder.getDispatchtime());
        cleaningWorkorder1.setFrequency(cleaningWorkorder.getFrequency());
        cleaningWorkorder1.setExecutionstatus(1);

        // 调用Mapper接口的添加方法，持久化清洁工单对象
        return cleaningWorkorderMapper.add(cleaningWorkorder1);
    }

    /**
     * 更新工单状态为已完成
     * @param id 工单ID
     * @return 更新影响的行数
     */
    @Override
    public int updata(Integer id) {
        CleaningWorkorder findbyid = cleaningWorkorderMapper.findbyid(id);
        findbyid.setGdzt(7);
        return cleaningWorkorderMapper.updata(findbyid);
    }

    /**
     * 根据代码查找清洁工单
     * @param code 工单代码
     * @return 查找到的清洁工单对象
     */
    @Override
    public CleaningWorkorder findcode(String code) {
        return cleaningWorkorderMapper.findcode(code);
    }

    /**
     * 更新工单状态为处理中
     * @param code 工单代码
     * @return 更新影响的行数
     */
    @Override
    public int handleupdate(String code) {
        CleaningWorkorder findcode = cleaningWorkorderMapper.findcode(code);
        findcode.setGdzt(4);
        return cleaningWorkorderMapper.handleupdate(findcode);
    }

    /**
     * 更新工单状态为有限处理
     * @param code 工单代码
     * @return 更新影响的行数
     */
    @Override
    public int limitupdate(String code) {
        CleaningWorkorder findcode = cleaningWorkorderMapper.findcode(code);
        findcode.setGdzt(3);
        return cleaningWorkorderMapper.limitupdate(findcode);
    }

    /**
     * 更新工单状态为处理完成
     * @param code 工单代码
     * @return 更新影响的行数
     */
    @Override
    public int dealupdate(String code) {
        CleaningWorkorder findcode = cleaningWorkorderMapper.findcode(code);
        findcode.setGdzt(2);
        findcode.setWctime(LocalDateTime.now());
        return cleaningWorkorderMapper.dealupdate(findcode);
    }

    /**
     * 评价工单
     * @param cleaningWorkorder 包含评价信息的工单对象
     * @return 更新影响的行数
     */
    @Override
    public int evaluate(CleaningWorkorder cleaningWorkorder) {
        return cleaningWorkorderMapper.evaluate(cleaningWorkorder);
    }

    /**
     * 将工单状态更新为挂起
     * @param id 工单ID
     * @return 更新影响的行数
     */
    @Override
    public int pending(Integer id) {
        CleaningWorkorder findbyid = cleaningWorkorderMapper.findbyid(id);
        findbyid.setGdzt(7);
        return cleaningWorkorderMapper.pending(findbyid);
    }

    /**
     * 派工操作，更新工单的工人信息
     * @param cleaningWorkorder 包含工人信息的工单对象
     * @return 更新影响的行数
     */
    @Override
    public int dispatcher(CleaningWorkorder cleaningWorkorder) {
        CleaningWorkorder findbyid = cleaningWorkorderMapper.findbyid(cleaningWorkorder.getId());
        findbyid.setWorker(cleaningWorkorder.getWorker());
        return cleaningWorkorderMapper.dispatcher(findbyid);
    }

    /**
     * 关闭工单
     * @param cleaningWorkorder 工单对象
     * @return 更新影响的行数
     */
    @Override
    public int close(CleaningWorkorder cleaningWorkorder) {
        return cleaningWorkorderMapper.close(cleaningWorkorder);
    }

    /**
     * 获取清洁工单列表
     * @return 清洁工单列表
     */
    @Override
    public List<CleaningWorkorder> listcleaning() {
        return cleaningWorkorderMapper.listcleaning();
    }

    /**
     * 获取维修工单列表
     * @return 维修工单列表
     */
    @Override
    public List<CleaningWorkorder> listrepair() {
        return cleaningWorkorderMapper.listrepair();
    }

    /**
     * 获取清洁任务列表
     * @return 清洁任务列表
     */
    @Override
    public List<CleaningWorkorder> listclean() {
        return cleaningWorkorderMapper.listclean();
    }

    /**
     * 根据执行状态获取工单列表
     * @param type 工单执行状态类型
     * @return 对应执行状态的工单列表
     */
    @Override
    public List<CleaningWorkorder> listexecutionstatus(Integer type) {
        return cleaningWorkorderMapper.listexecutionstatus(type);
    }

    /**
     * 根据名称查找工单列表
     * @param name 工单名称
     * @return 对应名称的工单列表
     */
    @Override
    public List<CleaningWorkorder> listname(String name) {
        return cleaningWorkorderMapper.listname(name);
    }

    @Override
    public List<CleaningWorkorder> listworker(String worker) {
        return cleaningWorkorderMapper.listworker(worker);
    }

    @Override
    public List<CleaningWorkorder> listcleanings(String worker) {
        return cleaningWorkorderMapper.listcleanings(worker);
    }

    @Override
    public List<CleaningWorkorder> listrepairs(String worker) {
        return cleaningWorkorderMapper.listrepairs(worker);
    }

    @Override
    public List<CleaningWorkorder> listcleans(String worker) {
        return cleaningWorkorderMapper.listcleans(worker);
    }

    @Override
    public List<CleaningWorkorder> listcleaningss(String name) {
        return cleaningWorkorderMapper.listcleaningss(name);
    }

    @Override
    public List<CleaningWorkorder> listrepairss(String name) {
        return cleaningWorkorderMapper.listrepairss(name);
    }

    @Override
    public List<CleaningWorkorder> listcleanss(String name) {
        return cleaningWorkorderMapper.listcleanss(name);
    }

    @Override
    public List<CleaningWorkorder> listtimes(Integer times, String worker) {
        return cleaningWorkorderMapper.listtimes(times, worker);
    }

    @Override
    public List<CleaningWorkorder> listtimess(Integer times, String name) {
        return cleaningWorkorderMapper.listtimess(times, name);
    }

    @Override
    public List<CleaningWorkorder> listyxjs(Integer yid, String worker) {
        return cleaningWorkorderMapper.listyxjs(yid, worker);
    }

    @Override
    public List<CleaningWorkorder> listyxjss(Integer yid, String name) {
        return cleaningWorkorderMapper.listyxjss(yid, name);
    }

    @Override
    public List<CleaningWorkorder> listgdlxs(Integer type, String worker) {
        return cleaningWorkorderMapper.listgdlxs(type, worker);
    }

    @Override
    public List<CleaningWorkorder> listgdlxss(Integer type, String name) {
        return cleaningWorkorderMapper.listgdlxss(type, name);
    }

    @Override
    public List<CleaningWorkorder> listexecutionstatuss(Integer type, String worker) {
        return cleaningWorkorderMapper.listexecutionstatuss(type, worker);
    }

    @Override
    public List<CleaningWorkorder> listexecutionstatusss(Integer type, String name) {
        return cleaningWorkorderMapper.listexecutionstatusss(type, name);
    }


}
